<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="应用名称" prop="softName">
        <el-input
          v-model="queryParams.softName"
          placeholder="请输入应用名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:post:edit']"
          >编辑</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
          >删除</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:post:export']"
          >导出</el-button
        >
      </el-col> -->
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table :data="postList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" type="index" align="center" />
      <!-- <el-table-column label="岗位编码" align="center" prop="postCode" /> -->
      <el-table-column label="应用名称" align="center" prop="softName" />
      <el-table-column label="所属" align="center" prop="dept" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="描述" align="center" prop="description" />
      <!-- <el-table-column label="岗位排序" align="center" prop="postSort" /> -->
      <!-- <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_normal_disable"
            :value="scope.row.status"
          />
        </template>
      </el-table-column> -->

      <!-- 创建时间 -->
      <!-- <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-folder"
            @click="handleMoos(scope.row)"
            v-hasPermi="['system:post:remove']"
            >版本管理</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="760px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="应用名称" prop="softName">
          <el-input v-model="form.softName" placeholder="请输入应用名称" />
        </el-form-item>
        <el-form-item label="所属" prop="dept">
          <el-input v-model="form.dept" placeholder="请输入编码名称" />
        </el-form-item>
        <!-- <el-form-item label="岗位顺序" prop="postSort">
          <el-input-number
            v-model="form.postSort"
            controls-position="right"
            :min="0"
          />
        </el-form-item> -->
        <!-- <el-form-item label="岗位状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="设置下载权限" prop="roleIds">
          <el-checkbox-group v-model="checkList">
            <el-checkbox label="角色 A"></el-checkbox>
            <el-checkbox label="角色 B"></el-checkbox>
            <el-checkbox label="角色 C"></el-checkbox>
            <el-checkbox label="角色 D"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">发布</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 版本管理 -->
    <el-dialog :visible.sync="op" width="1200px" append-to-body>
      <div class="app-container">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <!-- <el-form-item label="岗位编码" prop="postCode">
        <el-input
          v-model="queryParams.postCode"
          placeholder="请输入岗位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
          <el-form-item label="版本号" prop="version">
            <el-input
              v-model="queryParams.version"
              placeholder="请输入版本号"
              clearable
              @keyup.enter.native="handleQuery01"
            />
          </el-form-item>
          <!-- <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="岗位状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery01"
              >搜索</el-button
            >
            <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        > -->
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleCreat"
              v-hasPermi="['system:post:add']"
              >新增</el-button
            >
          </el-col>
          <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:post:edit']"
          >编辑</el-button
        >
      </el-col> -->
          <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
          >删除</el-button
        >
      </el-col> -->
          <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:post:export']"
          >导出</el-button
        >
      </el-col> -->
          <!-- <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar> -->
        </el-row>

        <el-table :data="dataList">
          <!-- <el-table-column type="selection" width="55" align="center" /> -->
          <el-table-column label="序号" type="index" align="center" />
          <!-- <el-table-column label="岗位编码" align="center" prop="postCode" /> -->
          <el-table-column label="版本号" align="center" prop="version" />
          <el-table-column label="URL" align="center" prop="postCode" />
          <el-table-column label="文件大小" align="center" prop="postName" />
          <!-- 创建时间 -->
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate02(scope.row)"
                v-hasPermi="['system:post:edit']"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete01(scope.row)"
                v-hasPermi="['system:post:remove']"
                >删除</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-thumb"
                @click="handleUpdate02(scope.row)"
                v-hasPermi="['system:post:remove']"
                >查看</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改岗位对话框 -->
        <el-dialog
          :title="title"
          :visible.sync="opoo"
          width="760px"
          append-to-body
        >
          <el-form
            ref="form"
            id="form"
            :model="form"
            :rules="rules"
            label-width="120px"
          >
            <el-form-item label="版本号" prop="version">
              <el-input v-model="form.version" placeholder="请输入版本号" />
            </el-form-item>
            <el-form-item label="URL">
              <el-input placeholder="请输入路径" />
            </el-form-item>
            <el-form-item label="文件大小">
              <el-input placeholder="请输入文件MB" />
            </el-form-item>
            <el-form-item label="文件上传" prop="file">
              <!-- <input type="file" ref="file" id="file" @change="fileChantge" /> -->
              <el-upload
                class="upload-demo"
                action="#"
                :on-change="uploadChange"
                multiple
                :limit="3"
                :file-list="fileList"
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">
                  只能上传jpg/png文件，且不超过500kb
                </div>
                <!-- <input type="file" ref="file" @change="fileChantge" /> -->
              </el-upload>
            </el-form-item>
            <el-form-item label="描述" prop="description">
              <el-input
                v-model="form.description"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm01">发布</el-button>
            <el-button @click="handleRid">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listManage,
  getManage,
  addManage,
  updateManage,
  delManage,
} from "@/api/serves/manage";

import {
  listVersion,
  getVersion,
  updateVersion,
  delVersion,
  addVersion,
} from "@/api/serves/version";
import { getToken } from "@/utils/auth";
import Axios from "axios";

// 下载权限
import { addRole } from "@/api/system/role";

export default {
  name: "Post",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示版本管理中的弹出层
      op: false,
      // 是否展示版本管理中的增加版本
      opoo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // postCode: undefined,
        postName: undefined,
        status: undefined,
        softManageId: undefined,
        // 版本号
        version: undefined,
        // 文件上传
        file: undefined,
        // 版本描述
        description: undefined,
      },
      // 表单参数
      form: {
        // 版本号
        version: undefined,
        // 文件上传
        file: undefined,
        // 版本描述
        description: undefined,
      },
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "应用名称不能为空", trigger: "blur" },
        ],
      },
      // 选择角色
      checkList: [],
      // 版本管理id
      softManageId: [],
      fileList: [],
    };
  },
  created() {
    this.getList();
    this.getList01();
  },
  methods: {
    //文件上传测试
    uploadChange(file, fileList) {
      this.form.file = file.raw;
      this.fileList.push(file);
      // formData.append("version", this.from.version);
      // formData.append("file", this.from.file);
      // formData.append("description", this.from.description);
      console.log(this.form, "545555");
      // console.log(e.target.files[0], "eeeeeeeeeeeeeeeeee");
      // let file = e.target.files[0];
      // if (file.size > 1024 * 500) {
      //   return this.$message.error("最大上传500KB");
      // }
      // console.log(file.name.split(".")[1] == "xlsx");
      // if (
      //   file.name.split(".")[1] != "xlsx" &&
      //   file.name.split(".")[1] != "xls"
      // ) {
      //   return this.$message.error("请上传表格文件");
      // }
      // //转换blob流
      // this.form.file = new Blob([file]);
      // // 添加父页面ID、
      // this.queryParams.softManageId = row.id;
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listManage(this.queryParams).then((response) => {
        if (response.code == "200") {
          this.postList = response.rows;
          this.total = response.total;
        } else {
          this.$message.error(response.msg);
        }
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        // id: undefined,
        // postCode: undefined,
        // postName: undefined,
        // postSort: 0,
        // status: "0",
        remark: undefined,
        // createBy: undefined,
        // createTime: undefined,
        dept: undefined,
        softName: undefined,
      };
      this.checkList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParams.softName = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    // resetQuery() {
    //   this.resetForm("queryForm");
    //   this.handleQuery();
    // },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.checkList = [];
      this.open = true;
      this.title = "新增软件应用";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getManage(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑岗位";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(this.form, this.checkList, "cacaacacacaacac");
          if (this.form.id != undefined) {
            updateManage(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManage(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
            // 下载权限
            addRole(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？')
        .then(function () {
          return delManage(postIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    // handleExport() {
    //   this.download(
    //     "system/post/export",
    //     {
    //       ...this.queryParams,
    //     },
    //     `post_${new Date().getTime()}.xlsx`
    //   );
    // },
    /** 查询版本列表 */
    getList01() {
      this.loading = true;
      listVersion(this.queryParams).then((response) => {
        if (response.code == "200") {
          this.dataList = response.rows;
          this.total = response.total;
        } else {
          this.$message.error(response.msg);
        }
        this.loading = false;
      });
    },
    /** 版本管理 **/
    handleMoos(row) {
      this.reset01();
      this.op = true;
      console.log("---------0000000000000", row);
      this.queryParams.softManageId = row.id;
      console.log("111111111111111111", this.queryParams);
      listVersion(this.queryParams).then((response) => {
        if (response.code == "200") {
          this.dataList = response.rows;
          this.total = response.total;
        } else {
          this.$message.error(response.msg);
        }
        this.loading = false;
      });
      this.title = "版本管理";
    },
    // 版本管理列表重置
    reset01() {
      this.form = {
        createBy: undefined,
        version: undefined,
        createTime: undefined,
        id: undefined,
        description: undefined,
      };
      this.checkList = [];
      this.resetForm("form");
    },
    /** 版本管理新增版本 **/
    handleCreat() {
      this.reset01();
      this.opoo = true;
    },
    // 版本管理-新增取消按钮
    handleRid() {
      this.opoo = false;
      this.reset01();
    },
    // 版本管理-新增提交按钮
    submitForm01() {
      let formData = new FormData();
      formData.append("version", this.form.version);
      formData.append("file", this.form.file);
      formData.append("description", this.form.description);
      console.log(formData, "formDataformDataformDataformData");
      let config = {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      };
      config.headers["Authorization"] = "Bearer " + getToken();
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            this.$axios
              .post("/soft/version", this.form, config, fromData)
              .then((res) => {
                if ((res.status = "200")) {
                  this.opoo = false;
                  this.getList01();
                  this.message({
                    message: "修改成功",
                    type: "success",
                  });
                }
              });
            console.log(this.form);
            // updateVersion(this.form, this.config,fromData).then((response) => {
            //   this.$modal.msgSuccess("修改成功");
            //   this.opoo = false;
            //   this.getList01();
            // });
          } else {
            console.log(Axios.post);
            Axios.put("/dev-api/soft/version", formData, config).then((res) => {
              console.log(res, "resresresres");
            });
            // this.$axios("/soft/version", this.form, config, fromData)
            //   .then((res) => {
            //     if ((res.status = "200")) {
            //       this.opoo = false;
            //       this.getList01();
            //       this.message({
            //         message: "新增成功",
            //         type: "success",
            //       });
            //     }
            //   });
            // addVersion(formData).then((response) => {
            //   this.$modal.msgSuccess("新增成功");
            //   this.opoo = false;
            //   this.getList01();
            // });
          }
        }
      });
    },
    // 版本管理-搜索
    handleQuery01() {
      // this.queryParams.softName = 1;
      this.getList01();
    },
    /** 版本管理-修改按钮操作 */
    handleUpdate02(row) {
      this.reset01();
      const id = row.id || this.ids;
      getVersion(id).then((response) => {
        this.form = response.data;
        this.opoo = true;
        this.title = "编辑版本";
      });
    },
    // 版本管理-删除按钮操作
    handleDelete01(row) {
      const postIds = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？')
        .then(function () {
          return delVersion(postIds);
        })
        .then(() => {
          this.getList01();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
